package com.gxa.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.gxa.common.base.R;
import com.gxa.common.util.MD5Utils;
import com.gxa.domain.dto.*;
import com.gxa.domain.entity.SysUser;
import com.gxa.domain.entity.SysUser1;
import com.gxa.domain.vo.LoginUserInfoVO;
import com.gxa.mapper.SysUserMapper;
import com.gxa.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public R login(LoginFormDTO loginFormDTO) {
        // 加密
        String newPwd = MD5Utils.encrypt(loginFormDTO.getPassword());
//        String newPwd = loginFormDTO.getPassword();
        SysUser sysUser = sysUserMapper.findSysUserByAccount(loginFormDTO.getAccount());
        if(sysUser != null && sysUser.getPassword().equals(newPwd)){
             // 对象拷贝
             LoginUserInfoVO loginUserInfoVO =  BeanUtil.copyProperties(sysUser,LoginUserInfoVO.class);
             return R.success("登录成功",loginUserInfoVO);
        }else{
             return R.error("账户或密码有误");
        }


    }

    @Override
    public R findSysUserByUserId(Integer userId) {
        SysUser sysUser = sysUserMapper.findSysUserByUserId(userId);
        return R.success(sysUser);
    }

    @Override
    public R addSysUser(SysUserSaveDTO sysUserSaveDTO) {
        // 加密
        String newPwd = MD5Utils.encrypt(sysUserSaveDTO.getPassword());
        // 加密后的密文将原来的明文替换掉
        sysUserSaveDTO.setPassword(newPwd);
        // 调用Mapper，添加操作
        int result = sysUserMapper.addSysUser(sysUserSaveDTO);
        if(result > 0 ){
            return R.success("添加成功！");
        }
        return R.error();
    }

    @Override
    public R updateSysUserInfo(SysUserInfoUpdateDTO sysUserInfoUpdateDTO) {
        int result = sysUserMapper.updateSysUserInfo(sysUserInfoUpdateDTO);
        if(result > 0 ){
            return R.success("修改成功！");
        }
        return R.error();
    }


    @Override
    public R updateSysUser(SysUserUpdateDTO sysUserUpdateDTO) {
        int result = sysUserMapper.updateSysUser(sysUserUpdateDTO);
        if(result > 0 ){
            return R.success("修改成功！");
        }
        return R.error();
    }

    @Override
    public R delSysUserByUserId(Integer userId) {
        int result = sysUserMapper.delSysUserByUserId(userId);
        if(result > 0 ){
            return R.success("删除成功！");
        }
        return R.error();
    }

    @Override
    public R updatePassword(ResetPasswordDTO resetPasswordDTO) {
        SysUser sysUser = sysUserMapper.findSysUserByUserId(resetPasswordDTO.getUserId());
        if(sysUser != null){
            // 1. 判断原密码对不对
            String newPwd = MD5Utils.encrypt(resetPasswordDTO.getOldPassword());
            if(!sysUser.getPassword().equals(newPwd)){
                return R.error("原密码不正确，请重新输入");
            }
            // 2. 修改密码
            String newPassword = MD5Utils.encrypt(resetPasswordDTO.getPassword());
            int result = sysUserMapper.updatePassword(resetPasswordDTO.getUserId(),newPassword);
            if(result > 0 ){
                return R.success("修改成功！");
            }
        }
        return R.error();
    }

}
